package com.basic.controller;

import com.basic.feign.ShoppingCartFeign;
import com.basic.util.TokenUtil;
import com.code.domain.AjaxResult;
import com.code.entity.User;
import com.code.service.IUserService;
import com.shoppingcart.entity.Shoppingcart;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ApiOperation(value = "购物车",notes = "购物车")
@Controller
@RequestMapping(value = "/basic/shoppingCart")
@Slf4j
@CrossOrigin
public class ShoppingCartBasicController {

    @Autowired
    @Lazy
    private ShoppingCartFeign shoppingCartFeign;

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "通过openFeign的形式根据用户id查询购物车信息",notes = "通过openFeign的形式根据用户id查询购物车信息")
    @GetMapping(value = "/queryShoppingCartToUserId")
    public @ResponseBody AjaxResult queryShoppingCartToUserId( HttpServletRequest request, HttpServletResponse response){

        //根据用户userName 查询用户信息
        String username = TokenUtil.getMemberIdByJwtToken(request);
        User user = userService.queryUserName(username);
        AjaxResult ajaxResult = shoppingCartFeign.queryShoppingcartToUserId(user.getUserid());
        return AjaxResult.success("操作成功",ajaxResult.get("data"));
    }


    @ApiOperation(value ="添加购物车",notes = "添加购物车")
    @PostMapping(value = "/saveBasicShoppingCart")
    public @ResponseBody AjaxResult saveBasicShoppingCart(@RequestBody Shoppingcart shoppingcart,
                                                          HttpServletRequest request,
                                                          HttpServletResponse response){
        //获取用户信息
        String username = TokenUtil.getMemberIdByJwtToken(request);
        User user = userService.queryUserName(username);
        shoppingcart.setCustomerId(user.getUserid());
        AjaxResult ajaxResult = shoppingCartFeign.saveShoppingcart(shoppingcart);
        return AjaxResult.success("操作成功",ajaxResult.get("data"));
    }

}
